{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Y2eqHW0TK87s"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1ELfd3QtCipCubqoedxmLDyZLVq0Jg34R?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "\n",
        "### 🎯 Few-Shot Chain of Thought (Few-Shot-CoT) - Prompt Engineering\n",
        "\n",
        "Few-Shot-CoT is a prompting technique that provides examples of step-by-step reasoning before asking the model to solve a new problem.\n",
        "\n",
        "### Key points:\n",
        "\n",
        "* 🔑 **Core concept:** Uses 1-5 examples of reasoning chains to guide the model's approach to new problems.\n",
        "\n",
        "* 📝 **Structure:** Includes example problems, their step-by-step solutions, and then a new problem to solve.\n",
        "\n",
        "* 🌟 **Advantage:** Improves performance by demonstrating the desired reasoning process.\n",
        "\n",
        "* 💼 **Applications:** Complex problem-solving, mathematical reasoning, logical deductions.\n",
        "\n",
        "* 🚀 **Implementation:** Carefully select diverse, relevant examples that showcase the desired reasoning style.\n",
        "\n",
        "* ⚖️ **Challenges:** Choosing appropriate examples and avoiding biasing the model.\n",
        "\n",
        "* 📝 **Example:**\n",
        "  * [Example problem 1]\n",
        "    Step 1: [Reasoning]\n",
        "    Step 2: [Reasoning]\n",
        "  * Answer: [Solution]\n",
        "    Now, solve this new problem using the same approach: [New problem]\n",
        "\n",
        "* 📈 **Effectiveness:** Often outperforms zero-shot techniques, especially on complex tasks."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Bqh8LxF79vzC",
        "outputId": "9a3dd68a-22ed-42a3-e8a0-e386a8756b17"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/2.5 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[91m━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.4/2.5 MB\u001b[0m \u001b[31m10.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K   \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━\u001b[0m \u001b[32m1.9/2.5 MB\u001b[0m \u001b[31m27.3 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m25.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.9/121.9 kB\u001b[0m \u001b[31m10.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.9/50.9 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -q -U \\\n",
        "     langchain>=0.3.19 \\\n",
        "     langchain-groq>=0.2.4 \\\n",
        "     langchain-community>=0.3.18"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_2qHf6Yc9wfi"
      },
      "outputs": [],
      "source": [
        "from langchain_groq import ChatGroq\n",
        "from langchain.prompts import ChatPromptTemplate"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_r_VL8gc-amJ"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import getpass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "APFjx9O7E70X"
      },
      "source": [
        "### 🔑 Provide Groq API Key\n",
        "\n",
        "- [Groq API Key](https://console.groq.com/keys)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wzStHf57-coR",
        "outputId": "68a1e17f-a4c8-4f31-d1f5-8304ad148624"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ],
      "source": [
        "os.environ[\"GROQ_API_KEY\"] = getpass.getpass()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RSVkIRvQ92TI"
      },
      "outputs": [],
      "source": [
        "llm = ChatGroq(\n",
        "    model=\"llama-3.1-8b-instant\",\n",
        "    temperature=0.5\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uK_eyo2E-Ip4"
      },
      "outputs": [],
      "source": [
        "few_shot_cot_prompt = ChatPromptTemplate.from_messages(\n",
        "    [\n",
        "        (\"system\", \"You are an expert at solving problems step-by-step.\"),\n",
        "        (\"human\", \"\"\"Here are some examples of solving problems step-by-step:\n",
        "\n",
        "            Q: What is 17 x 23?\n",
        "            A: Let's break it down:\n",
        "            1) First, let's multiply 17 by 20: 17 x 20 = 340\n",
        "            2) Now, let's multiply 17 by 3: 17 x 3 = 51\n",
        "            3) Finally, we add these results: 340 + 51 = 391\n",
        "            Therefore, 17 x 23 = 391\n",
        "\n",
        "            Q: How many seconds are in a day?\n",
        "            A: Let's calculate step-by-step:\n",
        "            1) There are 24 hours in a day\n",
        "            2) Each hour has 60 minutes\n",
        "            3) Each minute has 60 seconds\n",
        "            4) So, we calculate: 24 x 60 x 60\n",
        "            5) 24 x 60 = 1,440\n",
        "            6) 1,440 x 60 = 86,400\n",
        "            Therefore, there are 86,400 seconds in a day.\n",
        "\n",
        "            Now, solve this problem step-by-step:\n",
        "            Q: {input}\n",
        "            A: Let's break it down:\"\"\"),\n",
        "    ]\n",
        ")\n",
        "\n",
        "chain = few_shot_cot_prompt | llm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pgzO9ksY-nbq",
        "outputId": "5c21403d-89eb-43ce-b616-01c49aea9d5f"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "To find the area of a circle, we can use the formula:\n",
            "\n",
            "Area = πr^2\n",
            "\n",
            "Where r is the radius of the circle.\n",
            "\n",
            "Let's plug in the value of the radius, which is 5cm:\n",
            "\n",
            "Area = π(5)^2\n",
            "= π(25)\n",
            "= 3.14(25)\n",
            "= 78.5\n",
            "\n",
            "So, the area of the circle with a radius of 5cm is approximately 78.5 square centimeters.\n"
          ]
        }
      ],
      "source": [
        "question = \"What is the area of a circle with radius 5cm?\"\n",
        "response = chain.invoke({\"input\": question})\n",
        "print(response.content)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Z_p_lr6j-ugg"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
